package com.tuanzi.loan.business.repository.family.impl;

import com.tuanzi.loan.business.entity.family.ClassificationInfo;
import com.tuanzi.loan.business.repository.family.ClassificationRepository;
import com.tuanzi.loan.business.repository.family.MemberRightsRepository;
import com.tuanzi.loan.business.repository.family.MemberRightsRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.family.MemberRightsRequest;
import com.tuanzi.loan.vo.response.family.MemberRightsResponse;
import lombok.Data;
import org.jooq.Condition;
import org.jooq.SelectField;
import org.jooq.SelectQuery;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_MEMBER_RIGHTS;

@Data
@Repository
public class MemberRightsRepositoryCustomImpl extends JOOQBaseRepositoryPlus implements MemberRightsRepositoryCustom {


    @Override
    public Page<MemberRightsResponse> list(MemberRightsRequest searchRequest, Pageable pageable) {
        SelectQuery selectQuery = getSelectQuery(searchRequest);

        return search(selectQuery,pageable,record -> {
            MemberRightsResponse response = new MemberRightsResponse();
            response.setId(record.get(0,Long.class));
            response.setClassification_name(record.get(1,String.class));
            response.setRights_name(record.get(2,String.class));
            response.setStatus(record.get(3,Integer.class));
            response.setSort(record.get(4,Integer.class));
            response.setUrl(record.get(5,String.class));
            response.setCreate_time(record.get(6,Date.class));
            response.setUpdate_time(record.get(7,Date.class));
            return response;
        });
    }

    public SelectQuery getSelectQuery(MemberRightsRequest searchRequest){

        Condition condition = DSL.trueCondition();
        List<SelectField<?>> selectFields = new ArrayList<>();

        selectFields.add(LOAN_MEMBER_RIGHTS.ID);
        selectFields.add(LOAN_MEMBER_RIGHTS.CLASSIFICATION_NAME);
        selectFields.add(LOAN_MEMBER_RIGHTS.RIGHTS_NAME);
        selectFields.add(LOAN_MEMBER_RIGHTS.STATUS);
        selectFields.add(LOAN_MEMBER_RIGHTS.SORT);
        selectFields.add(LOAN_MEMBER_RIGHTS.URL);
        selectFields.add(LOAN_MEMBER_RIGHTS.CREATE_TIME);
        selectFields.add(LOAN_MEMBER_RIGHTS.UPDATE_TIME);

        if(!StringUtils.isEmpty(searchRequest.getRights_name())){
            condition = condition.and(LOAN_MEMBER_RIGHTS.RIGHTS_NAME.eq(searchRequest.getRights_name()));
        }
        if(null != searchRequest.getClassification_id()){
            condition = condition.and(LOAN_MEMBER_RIGHTS.CLASSIFICATION_ID.eq(searchRequest.getClassification_id()));
        }

        if(null != searchRequest.getStatus()){
            condition = condition.and(LOAN_MEMBER_RIGHTS.STATUS.eq(searchRequest.getStatus()));
        }
        if(null != searchRequest.getSort()){
            condition = condition.and(LOAN_MEMBER_RIGHTS.SORT.eq(searchRequest.getSort()));
        }
        if(!StringUtils.isEmpty(searchRequest.getUrl())){
            condition = condition.and(LOAN_MEMBER_RIGHTS.URL.eq(searchRequest.getUrl()));
        }

        return dslContext.select(selectFields).from(LOAN_MEMBER_RIGHTS)
                .where(condition).orderBy(LOAN_MEMBER_RIGHTS.SORT)
                .getQuery();
    }

    public SelectQuery getAllQuery(MemberRightsRequest searchRequest){

        return null;
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_MEMBER_RIGHTS;
    }
}
